/*
 * @Author: zhengzhuang
 * @Date: 2020-07-31 14:43:32
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-01-27 17:51:45
 * @Description: 订单状态管理
 * @FilePath: /C20Driver/lib/provider/order_information.dart
 */
import 'package:C20Driver/model/carpool_order_info.dart';
import 'package:C20Driver/model/carpool_passenger_order_info.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/config/service_api.dart';
import 'package:C20Driver/model/driver_receivables.dart';
import 'package:C20Driver/model/itinerary_order_info.dart';
import 'package:C20Driver/model/user_realtime_location_model.dart';
import 'package:C20Driver/provider/position_information.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/service_method.dart';
import 'package:C20Driver/utils/toast.dart';
import 'package:C20Driver/utils/utils.dart';
import 'package:provider/provider.dart';

class OrderInformation with ChangeNotifier {
  DateTime _currentTime = new DateTime.now();

  /// 获取当前时间
  String _orderId; // 订单ID
  ItineraryOrderInfo _orderInfo = ItineraryOrderInfo(); // 订单信息
  CarpoolOrderInfo _carpoolOrderInfo = CarpoolOrderInfo(); // 顺风车司机订单信息
  CarpoolPassengerOrderInfo _carpoolPassengerOrderInfo =
      CarpoolPassengerOrderInfo(); // 顺风车乘客订单信息
  List _carpoolPassengerList = []; //顺风车乘客列表
  get currentTime => _currentTime;
  get orderId => _orderId;
  get itineraryInfo => _orderInfo;
  get carpoolOrderInfo => _carpoolOrderInfo;
  get carpoolPassengerOrderInfo => _carpoolPassengerOrderInfo;
  get carpoolPassengerList => _carpoolPassengerList;

  setNullOrderid() {
    _orderInfo.orderId = null;
  }

  /// 设置订单信息
  setOrderInfo(
      {String orderId,
      String depAre,
      String depLongitude,
      String depLatitude,
      String destArea,
      String destLongitude,
      String destLatitude,
      String status,
      String passengerHeadimgurl,
      String passengerName,
      String passengerMobile,
      String lastNum,
      String intercityStatus,
      num peopleNumber,
      String depTime}) {
    if (orderId != null) _orderInfo.orderId = orderId;
    if (depAre != null) _orderInfo.depAre = depAre; // 出发点
    if (depLatitude != null) _orderInfo.depLatitude = depLatitude;
    if (depLongitude != null) _orderInfo.depLongitude = depLongitude;
    if (destArea != null) _orderInfo.destArea = destArea; // 目的地
    if (destLongitude != null) _orderInfo.destLongitude = destLongitude; // 目的地
    if (destLatitude != null) _orderInfo.destLatitude = destLatitude; // 目的地
    if (status != null) _orderInfo.status = status; // 订单状态
    if (passengerHeadimgurl != null)
      _orderInfo.passengerHeadimgurl = passengerHeadimgurl; // 用户头像
    if (passengerName != null) _orderInfo.passengerName = passengerName; // 用户名称
    if (passengerMobile != null)
      _orderInfo.passengerMobile = passengerMobile; // 用户手机号
    if (lastNum != null) _orderInfo.lastNum = lastNum; // 手机号后四位
    if (intercityStatus != null)
      _orderInfo.intercityStatus = intercityStatus; // 城际订单状态
    if (peopleNumber != null) _orderInfo.peopleNumber = peopleNumber; // 乘坐人数
    if (depTime != null) _orderInfo.depTime = depTime;
    notifyListeners();
  }

  /// 顺风车订单
  /// 当前选中时间
  DateTime _time = new DateTime.now();
  String _seatNum = ''; // 座位数
  String _departureTime = ''; // 出发时间显示
  String _departureTimeTransmission = ''; // 出发时间传输
  String _windmillStartingPoint = '请输入起点名称'; // 顺风车起点位置名称
  var _windmillStartingPointLocation = {}; // 顺风车起点位置
  String _startingPointDowntownArea = ''; // 顺风车起点省
  String _endOfWindmill = '你想要去哪儿？'; // 顺风车终点位置名称
  Map _endOfWindmillLocation = {}; // 顺风车终点位置
  String _terminalUrbanArea = ''; // 顺风车终点省

  String get seatNum => _seatNum;
  String get departureTime => _departureTime;
  String get departureTimeTransmission => _departureTimeTransmission;
  String get windmillStartingPoint => _windmillStartingPoint;
  get windmillStartingPointLocation => _windmillStartingPointLocation;
  String get startingPointDowntownArea => _startingPointDowntownArea;
  String get endOfWindmill => _endOfWindmill;
  get endOfWindmillLocation => _endOfWindmillLocation;
  String get terminalUrbanArea => _terminalUrbanArea;

  setCarpoolPassengerOrderInfo(CarpoolPassengerOrderInfo data) {
    _carpoolPassengerOrderInfo = data;
    // print(_carpoolPassengerOrderInfo.orderId);
    // print(_carpoolPassengerOrderInfo.passengerHeadimgurl);
  }

  setCarpoolPassengerList(List data) {
    _carpoolPassengerList = data;
  }

  /// 设置顺风车订单信息
  setCarpoolOrderInfo({
    String orderId,
    String depAre,
    String depLongitude,
    String depLatitude,
    String destArea,
    String destLongitude,
    String destLatitude,
    String state,
    String departureDate,
    num seatNum,
  }) {
    if (orderId != null) _carpoolOrderInfo.orderId = orderId;
    if (depAre != null) _carpoolOrderInfo.depAre = depAre; // 出发点
    if (depLatitude != null) _carpoolOrderInfo.depLatitude = depLatitude;
    if (depLongitude != null) _carpoolOrderInfo.depLongitude = depLongitude;
    if (destArea != null) _carpoolOrderInfo.destArea = destArea; // 目的地
    if (destLongitude != null)
      _carpoolOrderInfo.destLongitude = destLongitude; // 目的地
    if (destLatitude != null)
      _carpoolOrderInfo.destLatitude = destLatitude; // 目的地
    if (state != null) _carpoolOrderInfo.state = state; // 订单状态
    if (seatNum != null) _carpoolOrderInfo.seatNum = seatNum; // 乘坐人数
    if (departureDate != null) _carpoolOrderInfo.departureDate = departureDate;
    notifyListeners();
  }

  /// 获取今日时间
  void _setCarpoolTime() {
    _departureTime = '今天 ' +
        Utils.twoDigitTime(_currentTime.hour.toInt()) +
        ':' +
        Utils.twoDigitTime(_currentTime.minute.toInt());
    _departureTimeTransmission = _currentTime.year.toString() +
        '.' +
        _currentTime.month.toString() +
        '.' +
        _currentTime.day.toString() +
        ' ' +
        Utils.twoDigitTime(_currentTime.hour.toInt()) +
        ':' +
        Utils.twoDigitTime(_currentTime.minute.toInt());
    notifyListeners();
  }

  /// 获取今日时间
  get setCarpoolTime => _setCarpoolTime;

  /// 设置获取起点信息
  void _onCarpoolMap(UserRealtimeLocationModel latDataJson, context) {
    _windmillStartingPoint = latDataJson.name;
    _windmillStartingPointLocation['lat'] = latDataJson.lat;
    _windmillStartingPointLocation['lng'] = latDataJson.lng;
    _startingPointDowntownArea = latDataJson.cityName;
    Provider.of<PositionInformation>(context, listen: false)
        .setCityName(latDataJson.cityName);
    notifyListeners();
  }

  /// 设置获取起点信息
  get onCarpoolMap => _onCarpoolMap;

  /// 修改座位数
  void _modifiedSeatNumber(context) async {
    final option = await showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Container(
              color: Colors.white,
              height: ScreenUtil().setWidth(650.0),
              child: ListView(
                children: <Widget>[
                  InkWell(
                      child: ListTile(title: Text('1座')),
                      onTap: () => Navigator.pop(context, '1')),
                  InkWell(
                      child: ListTile(title: Text('2座')),
                      onTap: () => Navigator.pop(context, '2')),
                  InkWell(
                      child: ListTile(title: Text('3座')),
                      onTap: () => Navigator.pop(context, '3')),
                  InkWell(
                      child: ListTile(title: Text('4座')),
                      onTap: () => Navigator.pop(context, '4')),
                  InkWell(
                      child: ListTile(title: Text('5座')),
                      onTap: () => Navigator.pop(context, '5')),
                  InkWell(
                      child: ListTile(title: Text('6座')),
                      onTap: () => Navigator.pop(context, '6')),
                ],
              ));
        });
    if (option != null) {
      _seatNum = option;
      notifyListeners();
    }
  }

  /// 修改座位数
  get modifiedSeatNumber => _modifiedSeatNumber;

  /// 选择修改出发时间
  void _chooseDepartureTime(context) {
    DatePicker.showDateTimePicker(context,
        showTitleActions: true,
        minTime: DateTime(
            _currentTime.year,
            _currentTime.month,
            _currentTime.day,
            _currentTime.hour,
            _currentTime.minute,
            _currentTime.second),
        maxTime: DateTime(_currentTime.year, _currentTime.month,
            _currentTime.day + 20, 23, 59, 59), onConfirm: (date) {
      if (date.month == _currentTime.month && date.day == _currentTime.day) {
        _departureTime = '今天 ' +
            Utils.twoDigitTime(date.hour.toInt()) +
            ':' +
            Utils.twoDigitTime(date.minute.toInt());
      } else {
        _departureTime = date.month.toString() +
            '月' +
            date.day.toString() +
            '号 ' +
            Utils.twoDigitTime(date.hour.toInt()) +
            ':' +
            Utils.twoDigitTime(date.minute.toInt());
      }
      _departureTimeTransmission = date.year.toString() +
          '.' +
          date.month.toString() +
          '.' +
          date.day.toString() +
          ' ' +
          Utils.twoDigitTime(date.hour.toInt()) +
          ':' +
          Utils.twoDigitTime(date.minute.toInt());
      _time = date;
      notifyListeners();
    },
        currentTime: DateTime(_time.year, _time.month, _time.day, _time.hour,
            _time.minute, _time.second),
        locale: LocaleType.zh);
  }

  /// 修改座位数
  get chooseDepartureTime => _chooseDepartureTime;

  /// 设置顺风车起点信息
  // void _setCarpoolStartInfo(UserRealtimeLocationModel dataInfo) {
  //   _windmillStartingPoint = dataInfo.name;
  //   _windmillStartingPointLocation['lat'] = dataInfo.lat;
  //   _windmillStartingPointLocation['lng'] = dataInfo.lng;
  //   _startingPointDowntownArea = dataInfo.cityName;
  // }

  // /// 设置顺风车起点信息
  // get setCarpoolStartInfo => _setCarpoolStartInfo;

  /// 设置顺风车终点信息
  void _setCarpoolEndInfo(UserRealtimeLocationModel dataInfo) {
    _endOfWindmill = dataInfo.name;
    _endOfWindmillLocation['lat'] = dataInfo.lat;
    _endOfWindmillLocation['lng'] = dataInfo.lng;
    _terminalUrbanArea = dataInfo.cityName;
  }

  /// 设置顺风车终点信息
  get setCarpoolEndInfo => _setCarpoolEndInfo;

  /// 清空顺风车起始位置信息
  void _cleanCarpoolLocalInfo() {
    _endOfWindmill = "请输入终点名称";
    _endOfWindmillLocation = {};
    _terminalUrbanArea = '';
    _seatNum = '';
    _setCarpoolTime();
  }

  get cleanCarpoolLocalInfo => _cleanCarpoolLocalInfo;

  String _codeUrl; // 二维码收货地址
  String _amountCollected; // 收款金额
  String get codeUrl => _codeUrl;
  String get amountCollected => _amountCollected;

  /// 生成司机收款二维码
  getDriverReceivables(context, String number) {
    if (num.parse(number) == 0) return ToastX.center('请修改收款金额');
    DioUtils().request(ReqMethod.POST, ServiceApi.driverReceivables, params: {
      'amount': number,
      'business_code': 'A0870',
    }, success: (res) async {
      DriverReceivables dataModel = DriverReceivables.fromJson(res);
      _codeUrl = dataModel.codeUrl;
      _amountCollected = number;
      notifyListeners();
      NavigatorUtil.jump(context, '/intercityQrCodeCollection');
    });
  }
}
